package com.attendance.service.impl;

import com.attendance.common.CommonPage;
import com.attendance.common.CommonResult;
import com.attendance.mapper.StudentMapper;
import com.attendance.pojo.Pages;
import com.attendance.pojo.Student;
import com.attendance.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    //    根据条件查学生信息
    @Override
    public CommonResult selectStudent(Student student, Pages pages) {
        PageHelper.startPage(pages.getPage(),pages.getNum());
        return CommonResult.success(CommonPage.restPage(studentMapper.selectStudent(student)));
    }


    //    新增学生信息
    @Override
    public CommonResult selectStudentById(int id) {
        Student student = studentMapper.selectStudentById(id);
        return CommonResult.success(student,"查询成功！");
    }
    //    更改学生状态
    @Override
    public CommonResult updateStudentInfo(Student student) {
        int i = studentMapper.updateStudentInfo(student);
        if (i==1){
            return CommonResult.success("修改信息成功！");
        }else {
            return CommonResult.failed("修改数据失败！");
        }
    }
    //    根据学生id查学生
    @Override
    public CommonResult modifyStudentStatus(Student student) {
        int i = studentMapper.modifyStudentStatus(student);
        if (i==1){
            return CommonResult.success("修改信息成功！");
        }else {
            return CommonResult.failed("修改数据失败！");
        }
    }
    //    更新学生基本信息
    @Override
    public CommonResult addStudentInfo(Student student) {
        int i = studentMapper.addStudentInfo(student);
        if (i==1){
            return CommonResult.success("修改信息成功！");
        }else {
            return CommonResult.failed("修改数据失败！");
        }
    }

    //    根据课程id查询学生
    @Override
    public CommonResult selectStudentInfoByCourseId(int id, Pages pages) {
        PageHelper.startPage(pages.getPage(),pages.getNum());
        List<Student> students = studentMapper.selectStudentInfoByCourseId(id);
        return CommonResult.success(CommonPage.restPage(students));
    }

    //修改图片地址
    @Override
    public CommonResult upLoadImg(MultipartFile myfile, int id) {
        String path = "http://8.130.104.8:8080/uploadfiles/";
        String type = myfile.getOriginalFilename().substring(myfile.getOriginalFilename().lastIndexOf("."));
        String filename= UUID.randomUUID().toString()+type;
        try{
            Client client = Client.create();
            WebResource webResource = client.resource(path +"/" + URLEncoder.encode(filename,"utf-8"));
            webResource.put(myfile.getBytes());
        }catch(Exception ex){
            System.out.println("上传失败");
        }

        int i = studentMapper.upLoadImg(id,path+filename);
        if (i==1){
            return CommonResult.success("修改信息成功！");
        }else {
            return CommonResult.failed("修改数据失败！");
        }
    }
}
















